import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MainWindow extends JFrame {
    public JPanel main;
    private JPanel firstJPanel;
    private JPanel SecondJPanel;
    private JButton prveButton;
    private JButton nextButton;
    private JLabel homeImage;
    private JLabel themeImage;
    private JButton beginGame;
    private JLabel themeLabel;
    private JLabel zhuTi;
    public GamePanel gamePanel;

    public MainWindow() {
        Sound.background();
        this.setBounds(340, 150, 820, 300);
        this.setResizable(false);
        this.setTitle("恐龙快跑");
        this.addListener();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        main.add(firstJPanel, "firstJPanel");


        // 设置字体大小
        Font font = new Font("宋体", Font.BOLD, 25);
        zhuTi.setFont(font);
        font = new Font("宋体", Font.BOLD, 15);
        themeLabel.setFont(font);

        // homeImage设置背景红色
        homeImage.setBackground(Color.RED);

        prveButton.addActionListener(e -> {
            setIcon(themeImage, "image/k1.png", 100, 100);
            themeLabel.setText("经典小恐龙");
        });
        nextButton.addActionListener(e -> {
            setIcon(themeImage, "image/kun1.png", 100, 100);
            themeLabel.setText("坤坤");
        });
        this.initFirst();
        beginGame.addActionListener(e -> initSecond());

        // 窗口焦点监听器
        this.addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowLostFocus(WindowEvent e) {
                if (gamePanel != null) gamePanel.paused();
            }

            @Override
            // 获得焦点
            public void windowGainedFocus(WindowEvent e) {
                if (gamePanel != null) gamePanel.resume();
            }
        });
    }

    public void setIcon(JLabel label, String filename, int width, int height) {
        label.setSize(100, 100);
        label.setHorizontalAlignment(JLabel.CENTER);
        ImageIcon originalIcon = new ImageIcon(filename);
        Image image = originalIcon.getImage();
        Image scaledImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        ImageIcon scaledIcon = new ImageIcon(scaledImage);
        label.setIcon(scaledIcon);
    }

    public void initFirst() {
        this.setBounds(340, 150, 820, 300);
        CardLayout cardLayout = (CardLayout) main.getLayout();
        cardLayout.show(main, "firstJPanel");
        setIcon(homeImage, "image/home.png", 284, 160);
        prveButton.doClick();
    }

    public void initSecond() {
        ((CardLayout) main.getLayout()).next(main);
        if (themeLabel.getText().equals("经典小恐龙")) {
            this.restart(0);
        } else {
            this.restart(1);
        }
        this.setBounds(340, 150, 820, 260);
        ScoreRecorder.init();
    }

    public void restart(int theme) {
        SecondJPanel.removeAll();
        gamePanel = new GamePanel(theme);
        SecondJPanel.setLayout(new CardLayout());
        SecondJPanel.add(gamePanel);
        this.addKeyListener(gamePanel);
        this.requestFocus();
        SecondJPanel.validate();
    }

    private void addListener() {
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                ScoreRecorder.saveScore();
            }
        });
    }

    public static void main(String[] args) {
        MainWindow frame = new MainWindow();
        frame.setContentPane(frame.main);
        frame.setVisible(true);
    }
}